<?php

/*
 * taux/load-index.phtml
 */
?>
<?php
$title		 = "Gestion des tauxs";
$this->headTitle($title);
// Variables
$codeList	 = $this->codeList;
?>
<div class='content-body'>
    <table class='table table-striped  list-table table-manage-taux'>
        <thead>
            <td width='20%'>Poste</td>
            <td width='20%'>Taux horaire</td>
            <td width='20%'>Nouvelle valeur</td>
            <td width='20%'>Date d' application</td>            
            <td width='20%'></td>            
        </thead>
            
    
    <tbody>
        <?php if(count($tauxProc) == 0 && count($tauxProcApplied) == 0) : ?>  
        <!--tr><td >Aucun traitement à effectuer</td></tr-->
        <?php else: ?> 
            <?php 
                foreach ($tauxProc as $tp) :  
                $dateApply	 = new \DateTime($tp->date_application);
            ?>
            <tr>
                <td><?php printf("%s", $manager->getPosteLibelle($tp->poste_code)); ?></td>
                <td><?php echo $tp->taux ; ?></td>
                <td><a class='new-taux' type='text' data-name='nouveau_taux' data-pk='<?php echo $tp->id ; ?>' ><?php echo $tp->nouveau_taux ; ?> </a></td>
                <td><a class='new-taux-date_apply' data-name='date_application' data-pk='<?php echo $tp->id ; ?>'  data-type="combodate" data-format= "DD/MM/YYYY" data-template= 'D / MMMM / YYYY'>
                    <?php echo $dateApply->format('d/m/Y') ; ?></a></td>

                <td style="text-align: center;">
                    <a href='#' class='btn btn-success btn-small btn-apply-proiess' data-pk='<?php echo $tp->id; ?>'>Appliquer</a>
                    &nbsp;&nbsp;&nbsp;
                    <a class='del-taux' data-pk='<?php echo $tp->id; ?>' href='<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'erase')) ?>'>
                        <img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
                    </a>
                </td>
            </tr>    
            <?php    endforeach; ?>

                <?php if(count($tauxProc) && count($tauxProcApplied)) : ?>        
                   <tr><td colspan='4' style='background: white;'> </td></tr>
                <?php    endif; ?>    
                <?php 
                       foreach ($tauxProcApplied as $tp) :  $dateApply	 = new \DateTime($tp->date_application);
                ?>
                       <tr>
                           <td><?php printf("%s", $manager->getPosteLibelle($tp->poste_code)); ?></td>
                           <td><?php echo $tp->taux ; ?></td>
                           <td><?php echo $tp->nouveau_taux ; ?> </td>
                           <td><?php echo $dateApply->format('d/m/Y') ; ?></td>

                           <td style="text-align: center;">
                              <button class="btn btn-inverse  btn-small noEdit">Traitement appliqué</button>
                           </td>
                       </tr>    
                <?php    endforeach; ?>
               
            <?php endif; ?>        
    </tbody>
   
    </table>    
</div>
    <?php         
        //var_dump(date("y"));
    ?> 

<script type="text/javascript">
    $('.new-taux').editable({
        title : 'Nouveau taux',
        url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'saveNumberData')) ?>",
        validate : function (value){
            var isFloat = function(val){
                v = $.trim(val);
                patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                return patFloat.test(v);                     
            } ;
            value = $.trim(value);
            
            if(! isFloat(value)){
                return 'Données invalides.'
            }
        }
        });
      
    $('.new-taux-date_apply').editable({
        title : "Date d' application",  
        url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'saveStringData')) ?>",
        combodate : {
            minYear: <?php echo (date("Y")); ?>,
	    maxYear: 2025,
	    minuteStep: 1
        },
	params: function(params) {
		value = params.value;
		date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
		params.value = date.format('YYYY-MM-DD');
		return params;
	    }
        });
        
    $('.del-taux').on('click' ,function(e){
	    e.preventDefault();
            $its_btn = $(this);
	    pk = $(this).data('pk');
	    url = $(this).attr('href');
            
            if (!confirm('Voulez-vous annuler ce traitement?'))
		    return false;
                
	    ajax = $.ajax({
		type: 'post',
		url : url,
                data : '&pk=' + pk
	    });
	    ajax.success(function(data){
                /*
                $its_btn.parent().parent().fadeOut(200, function(){
                    $its_btn.remove();
                });*/
                window.location.reload();
	    });
	    ajax.error(function(){
		alert("Erreur");
	    });
	});
	
    $(".btn-apply-proiess").on('click', function (e){	
	e.preventDefault();
	
	rep = confirm ('Voulez-vous appliquer ce changement de taux horaire! ');
	
	if(!rep){
	    return 0;
	}
	// else
	
	pk = $(this).data('pk');
	
	$ajax = $.ajax({
	    method : 'post',
	    url : "<?php echo $this->url('RHX/default', array('controller'	 => 'taux', 'action'	 => 'apply')) ?>",
	    data : 'pk='+pk
	});
	$ajax.done(function(){
	    JsTaux.refresh();
	})
	
	    
    });
</script>

